package com.zou.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zou.mall.product.entity.CategoryEntity;
import com.zou.mall.product.service.CategoryService;
import com.zou.common.utils.PageUtils;
import com.zou.common.utils.R;



/**
 * 商品三级分类
 *
 * @author lingjin.zou
 * @email leonard_zou@163.com
 * @date 2022-02-14 14:32:42
 */
@Api("商品分类模块")
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    /**
     * 查询所有分类并将查到分类通过树状结构组装起来
     */
    @ApiOperation(value = "商品树状结构查询", notes = "商品树状结构查询")
    @GetMapping("/list/tree")
    public List<CategoryEntity> listTree1(){
        List<CategoryEntity> categoryEntities = categoryService.listWithTree();
        return categoryEntities;
    }

    @ApiOperation(value = "商品树状结构查询", notes = "商品树状结构查询")
    @GetMapping("/list/trees")
    public R listTree2(){
        //测试服务降级
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        List<CategoryEntity> categoryEntities = categoryService.listWithTree();
        R r = new R();
        r.setData(categoryEntities);
        return r;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
        boolean save = categoryService.save(category);
        R r = new R();
        r.setData(save);
        return r;
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @ApiOperation(value = "级联更新所有数据", notes = "级联更新所有数据")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		//categoryService.updateById(category);
        categoryService.updateCascade(category);
        return R.ok();
    }

    /**
     * 删除
     * @RequestBody 必须使用Post请求
     * SpringMVC会自动将请求体转换为json数据,转为对应的对象
     */
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @ApiOperation(value = "批量删除", notes = "批量删除")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //删除之前需要判断待删除的菜单那是否被别的地方所引用。
//		categoryService.removeByIds(Arrays.asList(catIds));
        //categoryService.removeMenuByIds(Arrays.asList(catIds));
        //批量逻辑删除就是批量更新字段状态
        categoryService.removeBatchIdsLogic(Arrays.asList(catIds));
        return R.ok();
    }

}
